﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class LogisticSearch : PageBase
    {
        SignBO sbo = new SignBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnSubmit_Click(null,null);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (txtPartNo.Text.Trim() != "")
                sql += " and bdntpartno='"+txtPartNo.Text.Trim()+"' ";
            if (txtPartName.Text.Trim() != "")
                sql += " and bdntpartname='"+txtPartName.Text.Trim()+"' ";
            if (txtSupplierName.Text.Trim() != "")
                sql += " and suppliername='"+txtSupplierName.Text.Trim()+"' ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and suppliercode='"+txtSupplierNo.Text.Trim()+"' ";
            if (txtStart.Text.Trim() != "")
                sql += " and trunc(receivedate)>=to_date( '" + txtStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtEnd.Text.Trim() != "")
                sql += " and trunc(receivedate)<=to_date( '" + txtEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            DataSet ds = sbo.GetSignListForSearch(sql);
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        public static DataTable GetPageTable(DataTable dt, int pageSize, int pageIndex)
        {
            if (dt != null)
            {
                DataTable dt1 = new DataTable();

                int pageEnd = pageIndex * pageSize;
                int pagebegin = (pageIndex - 1) * pageSize;


                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dt1.Columns.Add(new DataColumn(dt.Columns[i].ColumnName));
                }

                for (int i = pagebegin; i < pageEnd; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        dt1.ImportRow(dt.Rows[i]);
                    }

                }
                return dt1;
            }
            else
                return null;
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (txtPartNo.Text.Trim() != "")
                sql += " and bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtPartName.Text.Trim() != "")
                sql += " and bdntpartname='" + txtPartName.Text.Trim() + "' ";
            if (txtSupplierName.Text.Trim() != "")
                sql += " and suppliername='" + txtSupplierName.Text.Trim() + "' ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and suppliercode='" + txtSupplierNo.Text.Trim() + "' ";
            if (txtStart.Text.Trim() != "")
                sql += " and trunc(receivedate)>=to_date( '" + txtStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtEnd.Text.Trim() != "")
                sql += " and trunc(receivedate)<=to_date( '" + txtEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            DataSet ds = sbo.GetSignListForSearch(sql);
            ds.Tables[0].Columns.Remove("ID");
            ds.Tables[0].Columns.Remove("RECEIVEQUANTITY");
            ds.Tables[0].Columns["SIGNPROCESSOR"].ColumnName = "Receiver";
            CreateExcel(ds, "Logistic");
        }
    }
}